<?php
declare (strict_types=1);

namespace app\admin\controller\article;

use app\admin\model\article\ArticleComments;
use app\admin\model\article\ArticleUsers;
use app\common\controller\AdminBaseController;
use think\Request;
use app\admin\service\ExcelService;
use think\Exception;
use think\facade\Db;
use app\common\service\FormBuilder as Form;
use think\facade\Route;

class ArticleComment extends AdminBaseController
{
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new ArticleComments();

        $this->assign("user",ArticleUsers::where("status","=",1)->select()->toArray());
    }


    /**
     * @param $id
     * @return string|\think\response\Json
     * @throws \Exception
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:
     */
    public function edit($id){
        $info = $this->model->where("id", $id)->find();
        if (!$info) {
            return parent::failed("未查询到数据");
        }
        $f = [];
        $f[] = Form::textarea('content', '评论内容',$info->getData('content'))->rows(8)->info('不能为空')->placeholder("评论内容")->required("评论内容不能为空");
        $f[] = Form::hidden("id", $info->getData('id'));
        $form = Form::make_post_form('编辑评论', $f, Route::buildUrl('upAndAdd'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }


    /**
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:评论审核
     */
    public function check()
    {
        if (IS_AJAX) {
            $data = $this->request->post();
            try {
                if (is_array($data['ids'])) {
                    $id = $data['ids'];
                } else {
                    $id = @explode(",", $data['ids']);
                }
                $res = $this->model->where("id", "in", $id)->update(['status' => 1, 'is_show' => $data['is_show']]);
                if ($res){
                    return parent::JsonReturn("审核成功");
                }else{
                    return parent::JsonReturn("审核失败", 0);
                }
            } catch (Exception $exception) {
                return parent::JsonReturn($exception->getMessage(), 0);
            }
        }
    }


    /**
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:数据导出
     */
    public function export(){
        $param = $this->request->post();
        $where = [];
        if (!empty($param['isAsc']) && !empty($param['orderByColumn'])) {
            $order = "c.{$param['orderByColumn']} {$param['isAsc']}";
        }
        if (!empty($param['is_show'])) {
            $status = (int)$param['is_show'] - 1;
            $where[] = ['c.is_show', '=', $status];
        }

        if (!empty($param['status'])) {
            $status = (int)$param['status'] - 1;
            $where[] = ['c.status', '=', $status];
        }
        if (!empty($param['user_id'])) {
            $where[] = ['c.status', '=', $param['user_id']];
        }

        if (isset($param['time']) && $param['time'] != '') {
            $ck = @explode(" ~ ", $param['time']);
            $b = $ck[0] . " 00:00:00";
            $e = $ck[1] . " 23:59:59";
            $where[] = ['c.create_time', 'between', [strtotime($b), strtotime($e)]];
        }
        try {
            $data=$this->model->alias("c")
                ->join("articles_user u","u.id=c.user_id","left")
                ->join("articles a","c.article_id=a.id","left")
                ->field("c.*,a.title,u.nickname")
                ->where($where)->order($order)->select()->toArray();

            if (empty($data)) {
                return parent::JsonReturn("根据条件未查询到数据", 0);
            }
            $arr=[];
            foreach ($data as $k => $v) {
                $v['is_show'] == 1 ? $top = '通过审核' : $top = '未通过审核';
                $v['status'] == 1 ? $re = '已审核' : $re = '未审核';
                $arr[] = [
                    $v['id'], $v['title'], $v['nickname'], $v['content'], $re, $v['ip']
                    , $top, $v['browser'], $v['os'],$v['pid'],$v['create_time']
                ];
            }

        } catch (Exception $exception) {
            return parent::JsonReturn($exception->getMessage(), 0);
        }
        $filename = ExcelService::setExcelHeader(['ID', '所属文章', '用户名', '评论内容', '是否审核', 'IP', '是否通过审核', '浏览器', '系统','PID','评论时间'])
            ->setExcelTile('文章评论数据', '文章评论数据', date('Y-m-d H:i:s', time()))
            ->setExcelContent($arr)
            ->ExcelReturn();
        return parent::JsonReturn($filename);
    }


}
